package service

import (
	"database/sql"
	"errors"
	"gf-start-kit/app/model/admin"
	"gf-start-kit/app/model/auth_group_access"
	"gf-start-kit/library/base"
	"gf-start-kit/library/guid"
	"gf-start-kit/library/service"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

type AdminService struct {
}

type Admin struct {
	Username      string `v:"required|length:6,30#请输入账号|账号长度为:min到:max位"` // 用户名
	Nickname      string
	GroupAccessID int64
}
type AddReq struct {
	ID int64 `json:"id"`
	Admin
}
type EditReq struct {
	ID int64 `json:"id" v:"required"`
	Admin
}

func (s *AdminService) List(form *base.Form) ([]*admin.Entity, error) {
	where := " 1 = 1 "
	var params []interface{}
	if !g.IsEmpty(form.Params) {
		if !g.IsEmpty(form.Params["nickname"]) {
			where += " and nickname like ? "
			params = append(params, "%"+form.Params["nickname"]+"%")
		}
	}
	return admin.Model.Order(form.OrderBy).FindAll(where, params)
}
func (s *AdminService) Page(form *base.Form) ([]admin.Entity, error) {
	where := " 1 = 1 "
	var resData []admin.Entity
	var params []interface{}
	if !g.IsEmpty(form.Params) {
		if !g.IsEmpty(form.Params["nickname"]) {
			where += " and nickname like ? "
			params = append(params, "%"+form.Params["nickname"]+"%")
		}
	}
	num, err := admin.Model.As("t").FindCount(where, params)
	form.Total = num
	if err != nil {
		return resData, err
	}
	err = admin.Model.Where(where, params).Order(form.OrderBy).Page(form.Page, form.PageSize).M.Structs(&resData)
	if err != nil {
		return resData, err
	}
	return resData, nil
}
func (s *AdminService) Remove(id int64) (interface{}, error) {
	if id <= 0 {
		return new(admin.Entity), errors.New("参数不合法")
	}
	return admin.Model.Delete(" id = ?", id)
}
func (s *AdminService) Get(id int64) (*admin.Entity, error) {
	if id <= 0 {
		return new(admin.Entity), errors.New("参数不合法")
	}
	return admin.Model.FindOne(" id = ?", id)
}
func (s *AdminService) Save(req AddReq) (sql.Result, error) {
	req.ID = guid.NewUUID()
	if g.IsEmpty(req.GroupAccessID) {
		s.BindGroupAccess(req.ID, req.GroupAccessID)
	}
	return admin.Save(req)
}
func (s *AdminService) BindGroupAccess(adminId int64, groupAccessId int64) {
	m := auth_group_access.Model
	c, _ := m.Where(" uid = ?", adminId).Count()
	if c > 0 {
		//修改
		_, _ = m.Where(" uid = ? ", adminId).Update(g.Map{"group_id": groupAccessId})
	} else {
		//添加
		_, _ = m.Save(g.Map{"group_id": groupAccessId, "uid": adminId})
	}
}
func (s *AdminService) Update(req EditReq, tx ...*gdb.TX) (sql.Result, error) {
	if req.ID <= 0 {
		return nil, errors.New("ID不能为空")
	}
	if g.IsEmpty(req.GroupAccessID) {
		s.BindGroupAccess(req.ID, req.GroupAccessID)
	}
	m := admin.Model
	if len(tx) > 0 {
		m.TX(tx[0])
	}
	return m.Data(req).Where(admin.Columns.Id, req.ID).Update()
}
func (s *AdminService) GetLoginAdminInfo(r *ghttp.Request) *service.LoginInfo {
	resp := service.AdminGfToken.GetTokenData(r)
	var userInfo *service.LoginInfo
	_ = gconv.Struct(resp.Get("data"), &userInfo)
	return userInfo
}
